using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.Security.Permissions;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
using Spike.ServiceFramework.Audit.Entities;

namespace Spike.ServiceFramework.Audit
{
    /// <summary>
    /// An extension of the XMLWriterTraceListener that will create a new file after a specified duration or maximum size
    /// </summary>
    [HostProtection(Synchronization = true)]
    public class AuditTraceListener : TraceListener
    {
        public AuditTraceListener(string name) : base(name)
        {           

        }
        
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            if(data is ServiceModel.Audit.ServiceTraceRecord)
            {
                var trace = data as ServiceModel.Audit.ServiceTraceRecord;
                var audit = new Audit.Entities.Audit();
                
                var serviceTrace = new Spike.ServiceFramework.Audit.Entities.ServiceTrace
                             {
                                 Request = trace.Request,
                                 Reply = trace.Reply,
                                 URI = trace.URI,
                                 ApplicationDomain = trace.ApplicationDomain,
                                 Directory = trace.Directory,
                                 MachineName = trace.MachineName,
                                 UserName = trace.UserName,
                                 DomainName = trace.DomainName,
                                 CorrelationID = trace.CorrelationID,
                                 AfterReceiveRequest = trace.AfterReceiveRequest,
                                 BeforeSendReply = trace.BeforeSendReply,
                                 ContractName = trace.ContractName,
                                 ContractNamespace = trace.ContractNamespace,       
                                 ServiceTraceHeaders = new EntityCollection<ServiceTraceHeader>(),
                                 EventName = trace.EventName
                             };
                
                trace.Headers.ForEach(h => serviceTrace.ServiceTraceHeaders.Add(new ServiceTraceHeader { Header = h }));

                audit.ServiceTraces.AddObject(serviceTrace);               
                audit.SaveChanges();
            }

            if (data is ServiceModel.Audit.ClientTraceRecord)
            {
                var trace = data as ServiceModel.Audit.ClientTraceRecord;
                var audit = new Audit.Entities.Audit();

                var clientTrace = new Spike.ServiceFramework.Audit.Entities.ClientTrace                {
                    Request = trace.Request,
                    Reply = trace.Reply,
                    URI = trace.URI,
                    ApplicationDomain = trace.ApplicationDomain,
                    Directory = trace.Directory,
                    MachineName = trace.MachineName,
                    UserName = trace.UserName,
                    DomainName = trace.DomainName,
                    CorrelationID = trace.CorrelationID,
                    AfterReceiveReply = trace.AfterReceiveReply,
                    BeforeSendRequest = trace.BeforeSendRequest,
                    ClientTraceHeaders = new EntityCollection<ClientTraceHeader>(),
                    EventName = trace.EventName
                };

                trace.Headers.ForEach(h => clientTrace.ClientTraceHeaders.Add(new ClientTraceHeader { Header = h }));

                audit.ClientTraces.AddObject(clientTrace);
                audit.SaveChanges();
            }
        }


        public override void Write(string message)
        {
            // ignore
        }

        public override void WriteLine(string message)
        {
            // ignore
        }
    }

}